package com.fjec.common.aliyun.api.view;

import com.google.common.base.Strings;
import org.apache.commons.collections.MapUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 阿里云银行卡识别返回数据
 * {
 * "bank_name":"中国银行",      # 图片中没有的话，返回"" !!!
 * "card_num":"6227594409463185", !!!
 * "valid_date":"03/17",             # 图片中没有的话，返回""；如有多个，则逗号分隔开"03/17,04/05"
 * "card_type:"DC",       #DC(借记卡),  CC(贷记卡),  SCC(准贷记卡), DCC(存贷合一卡), PC(预付卡) !!!
 * "is_fake": false,                   #是否是复印件 !!!
 * "request_id":"20190806163620_e8b744b981620b764c4abb9537533287",
 * "success":true
 * }
 */
public class BankCardVO extends HashMap<String, Object> {

    private final static String SUCCESS_KEY = "success";
    private final static String BANK_NAME_KEY = "bank_name";
    private final static String CARD_NUM_KEY = "card_num";
    private final static String CARD_TYPE_KEY = "card_type";
    private final static String VALID_DATE_KEY = "valid_date";
    private final static String IS_FAKE_KEY = "is_fake";

    public static BankCardVO build(Map<String, Object> map) {
        BankCardVO vo = new BankCardVO();
        vo.putAll(map);
        return vo;
    }

    private BankCardVO() {
    }

    /**
     * 是否调用成功
     *
     * @return
     */
    public boolean isSuccess() {
        return MapUtils.getBooleanValue(this, SUCCESS_KEY);
    }

    /**
     * 银行名称
     *
     * @return
     */
    public String getBankName() {
        return MapUtils.getString(this, BANK_NAME_KEY);
    }

    /**
     * 卡号
     *
     * @return
     */
    public String getCardNum() {
        return MapUtils.getString(this, CARD_NUM_KEY);
    }

    /**
     * 类型
     * DC(借记卡),  CC(贷记卡),  SCC(准贷记卡), DCC(存贷合一卡), PC(预付卡)
     *
     * @return
     */
    public String getCardType() {
        return MapUtils.getString(this, CARD_TYPE_KEY);
    }

    /**
     * 有效期
     * 图片中没有的话，返回""；如有多个，则逗号分隔开"03/17,04/05"
     *
     * @return
     */
    public String getValidDate() {
        return MapUtils.getString(this, VALID_DATE_KEY);
    }

    /**
     * 是否是复印件
     *
     * @return
     */
    public boolean isFake() {
        return MapUtils.getBooleanValue(this, IS_FAKE_KEY);
    }

    /**
     * 校验证件有效性
     *
     * @return
     */
    public boolean isValid() {
        if (Strings.isNullOrEmpty(getBankName()) || Strings.isNullOrEmpty(getCardNum())
                || Strings.isNullOrEmpty(getCardType()) || isFake()) {
            return false;
        } else {
            return true;
        }
    }
}
